#include "Client.h"
#include "RS232.h"
#include "UI.h"
#include "../Common/Card.h"
#include "../ACUAS/ACUAS.h"

void
client_play()
{
  unsigned int chips = 0;
  int card_count = 0;

  while (1) {
    delay_ds(1);

    unsigned char command = rs232_get_command();
    if (!command)
      continue;

    if (command == 'B') {
      chips = rs232_get_uint();
      unsigned int bet = 100 + card_count * 2;
      if (bet < 25)
        bet = 25;

      rs232_send_bet(chips > bet ? bet : chips);

      ui_chips(chips);
    } else if (command == 'H') {
      unsigned char score = rs232_get_uchar();
      rs232_send_hit_or_stand(score > 16 ? 0 : 1);
    } else if (command == 'X') {
      rs232_skip();
    } else if (command == 'S') {
      chips = 0;
      card_count = 0;

      ui_chips(chips);
      ui_count(card_count);
    } else if (command == 'C') {
      Card card = rs232_get_uchar();
      unsigned value = card_get_value(card);
      // Revere RAPC
      if (value == 1)
        card_count -= 4;
      else if (value == 2 || value == 7)
        card_count += 2;
      else if (value == 3 || value == 4 || value == 6)
        card_count += 3;
      else if (value == 5)
        card_count += 4;
      else if (value == 9)
        card_count -= 1;
      else if (value >= 10)
        card_count -= 3;

      ui_count(card_count);
      ui_card(card);
    }
  }
}
